home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / native / implementing / example / example.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-13  |  1.8 KB  |  61 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. #include <native.h>
  18. #include "NativeExample.h"
  19.  
  20. static char *quotes[] = {
  21.     "The truth is out there -- X-Files",
  22.     "I suppose it will all make sense when we grow up -- Calvin & Hobbes",
  23.     "Who died and made you king? -- my dad"
  24. };
  25.  
  26. struct Hjava_lang_String *
  27. NativeExample_quote(struct HNativeExample *unused, long index)
  28. {
  29.     char *quotation;
  30.  
  31.     if (index < 1 || index > 3) {
  32.     SignalError(0, "java/lang/IllegalArgumentException", 0);
  33.     return NULL;
  34.     }
  35.  
  36.     quotation = quotes[index - 1];
  37.     return makeJavaString(quotation, strlen(quotation));
  38. }
  39.  
  40. long 
  41. NativeExample_twoTimes(struct HNativeExample *hInst)
  42. {
  43.     return unhand(hInst)->myValue * 2;
  44. }
  45.  
  46. struct HNativeExample *
  47. NativeExample_doubleUp(struct HNativeExample *hInst)
  48. {
  49.     HNativeExample *hNewInst;
  50.     long twoX;
  51.  
  52.     hNewInst = (HNativeExample *)execute_java_constructor(
  53.     0, "NativeExample", 0, "(I)", unhand(hInst)->myValue);
  54.  
  55.     twoX = (long)execute_java_dynamic_method(
  56.     0, (HObject *)hNewInst, "twoTimes", "()I");
  57.  
  58.     unhand(hNewInst)->myValue = twoX;
  59.     return hNewInst;
  60. }
  61.